<template>
  <div class="login-container">
    <div class="login-box">
      <div style="font-weight: bold; font-size: 24px; text-align: center; margin-bottom: 30px; color: #1450aa">欢 迎 注
        册
      </div>
      <el-form ref="formRef" :model="data.form" :rules="data.rules">
        <el-form-item prop="username">
          <el-input v-model="data.form.username" :prefix-icon="User" placeholder="请输入账号" size="large"/>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="data.form.password" :prefix-icon="Lock" placeholder="请输入密码" show-password
                    size="large"/>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input v-model="data.form.confirmPassword" :prefix-icon="Lock" placeholder="请确认密码" show-password
                    size="large"/>
        </el-form-item>
        <el-form-item>
          <el-button size="large" style="width: 100%" type="primary" @click="register">注 册</el-button>
        </el-form-item>
      </el-form>
      <div style="text-align: right;">
        已注册账号？请 <a href="/login">登录</a>
      </div>
    </div>

  </div>
</template>

<script setup>
import {reactive, ref} from "vue";
import {User, Lock} from "@element-plus/icons-vue";
import request from "@/utils/Request";
import {ElMessage} from "element-plus";
import router from "@/router";

const validatePass = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请确认密码'))
  } else if (value !== data.form.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const data = reactive({
  form: {role: 'USER'},
  rules: {
    username: [
      {required: true, message: '请输入账号', trigger: 'blur'},
    ],
    password: [
      {required: true, message: '请输入密码', trigger: 'blur'},
    ],
    confirmPassword: [
      {validator: validatePass, trigger: 'blur'},
    ],
  }
})


const formRef = ref()

// 点击注册按钮的时候会触发这个方法
const register = () => {
  formRef.value.validate((valid => {
    if (valid) {
      // 调用后台的接口
      request.post('/register', data.form).then(res => {
        if (res.code === '200') {
          ElMessage.success("注册成功")
          router.push('/login')
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
  })).catch(error => {
    console.error(error)
  })
}

</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #71b7e6, #9b59b6);
  background-size: cover;
  font-family: 'Arial', sans-serif;
}

.login-box {
  width: 400px;
  padding: 40px;
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 0.9);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  border: 1px solid #ddd;
}

.login-box h2 {
  margin-bottom: 30px;
  font-size: 28px;
  color: #333;
  text-align: center;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input {
  border: 1px solid #ccc;
  border-radius: 5px;
  padding: 10px 15px;
  font-size: 16px;
}

.el-input .el-input__inner {
  border: none !important;
}

.el-button {
  background-color: #409EFF;
  border-color: #409EFF;
  color: #fff;
  font-size: 18px;
  border-radius: 5px;
  padding: 10px 20px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.el-button:hover {
  background-color: #66b1ff;
}

.login-box a {
  color: #409EFF;
  text-decoration: none;
  transition: color 0.3s;
}

.login-box a:hover {
  color: #66b1ff;
}
</style>